package org.arch_learn.test.controller;

import org.arch_learn.flashboot.context.Autowired;
import org.arch_learn.mvc.annos.Controller;
import org.arch_learn.mvc.annos.RequestMapping;
import org.arch_learn.test.po.Account;
import org.arch_learn.test.service.DemoService;

import javax.servlet.http.HttpServletResponse;

@Controller
public class DemoController {

    @Autowired(value = "demoService")
    private DemoService demoService;

    //    @Security(names = {"张三"})
    @RequestMapping(value = "/get")
    public void get(String cardNo, HttpServletResponse httpServletResponse) {
        Account account = new Account();
        try {
            account = demoService.queryAccountByCardNo(cardNo);
            httpServletResponse.addHeader("msg", String.format("账户id:%s的信息为：%s", cardNo, account.toString()));
        } catch (Exception e) {
            httpServletResponse.addHeader("msg", String.format("获取账户id：%s信息失败！", cardNo));
        }
        System.out.println("------" + account.toString());
    }

    @RequestMapping(value = "/transfer")
    public void transfer(String from, String to, String amount, HttpServletResponse httpServletResponse) {
        try {
            demoService.transfer(from, to, amount);
            Account fromAcc = demoService.queryAccountByCardNo(from);
            Account toAcc = demoService.queryAccountByCardNo(to);
            httpServletResponse.addHeader("msg", String.format("交易成功！转出账户%s余额：%s，转入账户%s余额：%s", fromAcc.getCard_no(), fromAcc.getMoney(), toAcc.getCard_no(), toAcc.getMoney()));
        } catch (Exception e) {
            Account fromAcc = demoService.queryAccountByCardNo(from);
            Account toAcc = demoService.queryAccountByCardNo(to);
            httpServletResponse.addHeader("msg", String.format("交易失败！转出账户%s余额：%s，转入账户%s余额：%s", fromAcc.getCard_no(), fromAcc.getMoney(), toAcc.getCard_no(), toAcc.getMoney()));
        }

    }
}
